{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "72349801",
   "metadata": {},
   "source": [
    "# 实践：宿舍人脸识别门禁系统\n",
    ">1. 创建FaceSet\n",
    ">2. 查询FaceSet\n",
    ">3.存入face_token数据\n",
    ">4.获取FaceSet信息数据\n",
    ">5.人脸搜索\n",
    ">6.人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa411778",
   "metadata": {},
   "source": [
    "## 1.创建FaceSet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "cfbe699e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "17541228",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648477945,18bd0c84-1dc8-476b-82b7-0a181f3499f6',\n",
       " 'time_used': 29,\n",
       " 'faces': [{'face_token': '5dfd951cd8bd0c32cbc0a6878f307206',\n",
       "   'face_rectangle': {'top': 102, 'left': 128, 'width': 215, 'height': 215}}],\n",
       " 'image_id': 'aqu3wmGY+4iy/o05T1NI6w==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_Key,API_Secret,'p1.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "f7a242ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648477946,57c2f5f1-6856-41bf-8fa5-84ad457529b1',\n",
       " 'time_used': 76,\n",
       " 'faces': [{'face_token': '572c12f3b74edae7bf13cf3286afe872',\n",
       "   'face_rectangle': {'top': 261, 'left': 187, 'width': 320, 'height': 320}}],\n",
       " 'image_id': 'GNYkhj2eajY3UHihf0uLdQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_Key,API_Secret,'p2.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "a298af33",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648477946,76031aac-9f1c-46f9-a819-0bb171caed80',\n",
       " 'time_used': 63,\n",
       " 'faces': [{'face_token': '62fe640a6970015c10fb4e2fa13ede39',\n",
       "   'face_rectangle': {'top': 218, 'left': 254, 'width': 287, 'height': 287}}],\n",
       " 'image_id': 'tjPTJ6N06fvDbr3i1y0dlQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_Key,API_Secret,'p3.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "ffa71d1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648477947,b43af937-4f0f-4f10-abcc-cd6cf7c1e977',\n",
       " 'time_used': 74,\n",
       " 'faces': [{'face_token': '78067474d2bdfed0e5d62aea88733a80',\n",
       "   'face_rectangle': {'top': 219, 'left': 204, 'width': 296, 'height': 296}}],\n",
       " 'image_id': 'c/hHIpCdTpvPUPxISJDu8w==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_Key,API_Secret,'p4.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "7c810252",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_Key = 'lF2JmrX78SS6-VERJTIAolLXm8_B6Fv8'\n",
    "API_Secret = '3tIf7ThN8STzEisCsN277l2-AYXwD_pq'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "e6a41013",
   "metadata": {},
   "outputs": [],
   "source": [
    "FaceSet_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    'api_key':API_Key,\n",
    "    'api_secret':API_Secret,\n",
    "    'outer_id':\"dorm_faces\",\n",
    "    'display_name':\"西11\",\n",
    "    'tags':\"schoolgirl\",\n",
    "    'face_tokens':\"5dfd951cd8bd0c32cbc0a6878f307206,572c12f3b74edae7bf13cf3286afe872,62fe640a6970015c10fb4e2fa13ede39,78067474d2bdfed0e5d62aea88733a80\",\n",
    "    'user_data':\"4人\"\n",
    "}\n",
    "r = requests.post(url=FaceSet_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "e24a8f3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "5e939018",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'de6612cccb763ef1b4e5ccf9a36e662f',\n",
       " 'time_used': 924,\n",
       " 'face_count': 4,\n",
       " 'face_added': 4,\n",
       " 'request_id': '1648478035,a16575dc-4a73-458b-80ec-fcbc63d48c02',\n",
       " 'outer_id': 'dorm_faces',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1f42c7b",
   "metadata": {},
   "source": [
    "## 2.查询FaceSet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "2b14d4d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "Getdetail_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "payload = {\n",
    "    'api_key':API_Key,\n",
    "    'api_secret':API_Secret,\n",
    "    'faceset_token': 'de6612cccb763ef1b4e5ccf9a36e662f'\n",
    "}\n",
    "r = requests.post(url=Getdetail_url,params=payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "c32dfd9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "6b6d48a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'de6612cccb763ef1b4e5ccf9a36e662f',\n",
       " 'tags': 'schoolgirl',\n",
       " 'time_used': 96,\n",
       " 'user_data': '4人',\n",
       " 'display_name': '西11',\n",
       " 'face_tokens': ['572c12f3b74edae7bf13cf3286afe872',\n",
       "  '78067474d2bdfed0e5d62aea88733a80',\n",
       "  '62fe640a6970015c10fb4e2fa13ede39',\n",
       "  '5dfd951cd8bd0c32cbc0a6878f307206'],\n",
       " 'face_count': 4,\n",
       " 'request_id': '1648478066,c06c3908-aafb-4961-a841-a5a0427d26a5',\n",
       " 'outer_id': 'dorm_faces'}"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29872a07",
   "metadata": {},
   "source": [
    "## 3.存入face_token数据与第一步结合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f59da97a",
   "metadata": {},
   "source": [
    "## 4.获取FaceSet信息数据与第二步重合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2eb5d7f5",
   "metadata": {},
   "source": [
    "## 5.人脸搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "294d9e7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648478401,edb2ec51-bfca-4907-a6c7-b1d0c6e2b74e',\n",
       " 'time_used': 133,\n",
       " 'faces': [{'face_token': '4d529884347c33b5afb889a9a580f711',\n",
       "   'face_rectangle': {'top': 191, 'left': 172, 'width': 237, 'height': 237}}],\n",
       " 'image_id': 'Vo7MtkfOruLZBesrsuef+g==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_Key,API_Secret,'p5.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "c76d77e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648478481,cd31a8e3-b83d-4acf-9e44-aa8f1c584203',\n",
       " 'time_used': 493,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 65.656,\n",
       "   'user_id': '',\n",
       "   'face_token': '572c12f3b74edae7bf13cf3286afe872'}]}"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_url = \"https://api-cn.faceplusplus.com/facepp/v3/search\"\n",
    "payload = {\n",
    "    'api_key':API_Key,\n",
    "    'api_secret':API_Secret,\n",
    "    'face_token':'4d529884347c33b5afb889a9a580f711',\n",
    "    'outer_id':\"dorm_faces\"\n",
    "}\n",
    "\n",
    "r = requests.post(url = search_url, params = payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e96c3e09",
   "metadata": {},
   "source": [
    "## 6.人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "72bb10d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648478606,4eb7b632-62b0-4f5d-8340-ecdeafb62959',\n",
       " 'time_used': 271,\n",
       " 'confidence': 40.581,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare_url=\"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_Key,\n",
    "    'api_secret':API_Secret,\n",
    "    \"face_token1\":\"5dfd951cd8bd0c32cbc0a6878f307206\",\n",
    "    \"face_token2\":\"572c12f3b74edae7bf13cf3286afe872\"\n",
    "}\n",
    "r = requests.post(url = compare_url, params = payload)\n",
    "r.json()\n",
    "if d['confidence'] >= d['thresholds']['1e-5']:\n",
    "    print(\"是本宿舍成员\")\n",
    "else:\n",
    "    print(\"不是本宿舍成员\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a845e06",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "650bbefb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b844226",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
